local ImageButton = require "widgets/imagebutton"
local Text = require "widgets/text"
local UIAnim = require "widgets/uianim"

---comment
---@param m any
---@param id any
---@return ent|nil
local function checkTarContainer(m,id)
    return m.container and m.container.prefab == id and m.container or nil
end
---@class widget_containerwidget_webbert_skitter : widget_containerwidget
---@field elf_title widget_text
---@field elf_title_hp widget_text
---@field OnUpdate function|nil

AddClassPostConstruct('widgets/containerwidget',
---comment
---@param self widget_containerwidget_webbert_skitter
---@param ... unknown
function (self, ...)
    local old_Open = self.Open
    function self:Open(container, doer, ...)
        if old_Open ~= nil then
            old_Open(self, container, doer, ...)
        end
        local elf = checkTarContainer(self, "webbert_skitter")
        if elf then
            self.bganim:SetScale(1,1.2,1)
            if self.elf_title == nil then
                self.elf_title = self:AddChild(Text(BODYTEXTFONT,40,STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_skitter.title))
                self.elf_title:SetPosition(-80,125)
                self.elf_title_hp = self:AddChild(Text(BODYTEXTFONT,40,''))
                self.elf_title_hp:SetPosition(220,125)
                self.elf_title_hp:SetHAlign(ANCHOR_LEFT)
                self.elf_title_hp:SetRegionSize(500,50)
            end
        end
        self:StartUpdating()
    end

    local old_Close = self.Close
    function self:Close(...)
        if old_Close ~= nil then
            old_Close(self,...)
        end
        self:StopUpdating()
    end

    local old_OnUpdate = self.OnUpdate
    function self:OnUpdate(...)
        if old_OnUpdate ~= nil then
            old_OnUpdate(self,...)
        end
        local elf = checkTarContainer(self, "webbert_skitter")
        if elf then
            if self.elf_title_hp then
                local hobby = elf.replica.webbert_skitter_misc.hobby:value()
                local id = elf.replica.webbert_skitter_misc.hobby_id:value()
                self.elf_title_hp:SetString(STRINGS.MOD_WEBBER_THE_TRAINER.elf.hobby[hobby].id[id])
            end
        end
    end
end)

----------------------------------
-- 玩家干活, elf也干活

---comment
---@param player ent
---@param data event_data_working
local function onworking(player,data)
    local tar = data and data.target
    if tar and tar:HasTag('tree') then
        SUGAR_webber_the_trainer:taskDoInTime(player,'taskintime_webbert_stop_chop',10)
    end
    if tar and tar:HasTag('boulder') then
        SUGAR_webber_the_trainer:taskDoInTime(player,'taskintime_webbert_stop_mine',10)
    end
end

AddPlayerPostInit(function (inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:ListenForEvent('working',onworking)
end)